let app = getApp()
let NORMAL = 0, LOWER = 1, PULLDOWN = 2
Page({
    data: {
      curNav: 1,
      curIndex: 0,
      cateLeft: [],
      cateRight: [],
      imgUrl: app.globalData.imgUrl,
      statusType: [],
      currentType: 0,
      goodsList: [],
      everLoad: false,
      offset: 1,
      limit: 10,
      loading: false,
      loadingComplete: false,
      loaded: false,
      noConnect: false,
      topEnable: false
    },
    onLoad: function() {
    },
    

/**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    this.data.offset = 1
    this.setData({
      loading: false,
      loadingComplete: false,
      loaded: false,
      noConnect: false,
      topEnable: false
    })

    wx.showNavigationBarLoading()
    //if (this.data.statusType.length > 0) {
      

      //this.getGoodsList(this.data.statusType[this.data.currentType].id, PULLDOWN)
      this.getMenuList(PULLDOWN)
    //} else {
    //  this.setData({
    //    goodsList: [],
    //    loaded: true,
    //    topEnable: false
    //  })
    //}
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("toLower" + this.data.loadingComplete)
    this.setData({
      loading: this.data.loading,
      loadingComplete: this.data.loadingComplete
    })
    if (!this.data.loadingComplete) {
      this.data.offset += 1
      this.getGoodsList(this.data.statusType[this.data.currentType].id, LOWER)
    }
  },


  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index
   
    this.setData({
      currentType: curType
    });
    console.log(this.data.statusType[curType])
    this.setData({
      offset: 1,
      limit: 10,
      loading: false,
      loadingComplete: false,
      loaded: false,
      noConnect: false,
      topEnable: true
    })
    if (this.data.statusType.length > 0) {
      this.getGoodsList(this.data.statusType[curType].id, NORMAL)
    } else {
      that.setData({
        goodsList: [],
        loaded: true,
        topEnable: false
      })
    }
    
  },

  onShow() {
    this.getMenuList(NORMAL)
  },

  getMenuList: function (flag) {
    let token = wx.getStorageSync('token')
    var that = this;
    wx.request({
      url: app.apiUrl("/menu/index"),
      data: {
      },
      method: "GET",
      header: {
        "Authorization": 'Bearer ' + token
      },
      success: function (res) {
        console.log('getcategory')
        console.log(res.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })

          that.setData({
            goodsList: [],
            loaded: true,
            topEnable: false
          })

        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          if (res.data.data.length > 0) {
            that.setData({
              statusType: res.data.data,
              topEnable: true
            });
            that.getGoodsList(res.data.data[that.data.currentType].id, flag)
          } else {
            that.setData({
              goodsList: [],
              loaded: true,
              topEnable: false
            })
          }
        }

      }, fail: function (res) {
        that.setData({
          noConnect: true,
          topEnable: false
        })
        // wx.showModal({
        //   title: '提示',
        //   content: '网络请求异常',
        //   showCancel: false
        // })
      },
      complete: function (res) {
        that.setData({
          loaded: true
        })
        if (flag == PULLDOWN) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      }
    })
  },

  /**
   * 获取商品列表
   */
  getGoodsList: function (menuID, flag) {
    let token = wx.getStorageSync('token')
    let that = this
    if (flag == LOWER) {
      this.setData({
        loading: true,
      })
    }
    wx.request({
      url: app.apiUrl('/product/list'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        menu_id: menuID,
        page: that.data.offset,
        page_size: that.data.limit
      },
      success: function (res) {      
        console.log(res.data)
        let loadingComplete = false
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })

          that.setData({
            goodsList: [],
            loaded: true,
            topEnable: false
          })

        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 0)
        } else {
          for (var i = 0; i < res.data.data.length; i++) {
            res.data.data[i].pic_url = app.globalData.imgUrl + res.data.data[i].file.savepath + res.data.data[i].file.savename
            res.data.data[i].price = res.data.data[i].price.toFixed(2)
            res.data.data[i].old_price = res.data.data[i].old_price.toFixed(2)
          }
          if (!(res.data.data.length == that.data.limit)) {
            loadingComplete = true
          }

          that.setData({
            goodsList: flag == LOWER ? that.data.goodsList.concat(res.data.data) : res.data.data,
            loadingComplete: loadingComplete
          })  
        }
  
      },
      fail: function (res) {
        that.setData({
          noConnect: true
        })
        // wx.showModal({
        //   title: '提示',
        //   content: '网络请求异常',
        //   showCancel: false
        // })
      },
      complete: function (res) {
        that.setData({
          loaded: true
        })
        if (flag == LOWER) {
          that.setData({
            loading: false,
          })
        } else if (flag == PULLDOWN) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      }
    })
  },

  /**
   * 跳转到搜索页面
   */
  toSearch: function () {
    wx.navigateTo({
      url: "/pages/search/index?fromID=0"
    })
  },
  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id + "&type=0"
    })
  }
})